package cn.tutu.blockchain.controller;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.constraints.NotBlank;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;

import cn.tutu.blockchain.common.MsgObj;
import cn.tutu.blockchain.common.Page;
import cn.tutu.blockchain.minemachine.entity.Minemachine;
import cn.tutu.blockchain.minemachine.service.MinemachineService;
import cn.tutu.blockchain.sysuser.entity.SysUser;
import cn.tutu.blockchain.sysuser.service.SysUserService;

@RestController
@RequestMapping("/minemachine")
@Validated
public class MinemachineController {

	private static Logger LOGGER = LoggerFactory.getLogger(MinemachineController.class);

	@Autowired
	private MinemachineService minemachineService;
	
	@Autowired
	private SysUserService sysUserService;

	@RequestMapping("/create")
	public MsgObj create(@Validated Minemachine nodeInfo) {
		LOGGER.info("用户:{},绑定矿机:{}", nodeInfo.getUserId(), nodeInfo.getMineMachineId());
		minemachineService.create(nodeInfo);
		return MsgObj.buildSuccessMsg();
	}

	@RequestMapping("/modify")
	public MsgObj modify(Minemachine nodeInfo) {

		return MsgObj.buildSuccessMsg();
	}

	@RequestMapping("/remove")
	public MsgObj remove(@NotBlank(message = "用户ID不可为空") String userId,
			@NotBlank(message = "矿机ID不可为空") String mineMachineId) {
		LOGGER.info("用户:{},解绑矿机:{}", userId, mineMachineId);
		minemachineService.deleteByMacIdAndUserId(userId, mineMachineId);
		return MsgObj.buildSuccessMsg();
	}
	
	@RequestMapping("/removeByPhone")
	public MsgObj removeByPhone(@NotBlank(message = "用户ID不可为空!") String userId,@NotBlank(message = "手机号不可为空!") String phone,
			@NotBlank(message = "矿机ID不可为空!") String mineMachineId) {
		LOGGER.info("操作用户:{},解绑用户:{},解绑矿机:{}", userId,phone, mineMachineId);
		SysUser user = sysUserService.findByPhone(phone);
		
		if(null == user) {
			LOGGER.error("操作用户:{},解绑用户:{},解绑矿机:{},解绑失败,该手机号未注册", userId,phone, mineMachineId);
			return MsgObj.buildExceptionMsg("该手机号未注册!");
		}
		int i = minemachineService.deleteByMacIdAndUserId(user.getId(), mineMachineId);
		if(i <= 0 ) {
			LOGGER.error("操作用户:{},解绑用户:{},解绑矿机:{},解绑失败,该手机号与矿机未绑定!", userId,phone, mineMachineId);
			return MsgObj.buildExceptionMsg("该手机号与矿机未绑定!");
		}else {
			return MsgObj.buildSuccessMsg();
		}
	}

	/**
	 * 判断矿机是否已被用户绑定
	 * 
	 * @param mineMachineId
	 * @return
	 */
	@RequestMapping("/bind")
	public MsgObj bind(@NotBlank(message = "矿机ID不可为空") String mineMachineId) {
		Minemachine machine = minemachineService.getByMachineId(mineMachineId);
		if (null != machine) {
			if (StringUtils.isEmpty(machine.getUserId())) {
				return MsgObj.buildSuccessObj(null);
			}
		}
		return MsgObj.buildSuccessObj(machine);

	}

	@RequestMapping("/getGrid")
	public MsgObj getGrid(String userId, Page page) {
		PageInfo<Minemachine> byModel = minemachineService.getByUserId(userId, page);
		return MsgObj.buildSuccessObj(byModel.getList());
	}

}
